package case_library_backup

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/case_library_backup"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    case_library_backupReq "github.com/flipped-aurora/gin-vue-admin/server/model/case_library_backup/request"
)

type Case_struct_backupService struct {
}

// CreateCase_struct_backup 创建Case_struct_backup记录
// Author [piexlmax](https://github.com/piexlmax)
func (case_backupService *Case_struct_backupService) CreateCase_struct_backup(case_backup *case_library_backup.Case_struct_backup) (err error) {
	err = global.GVA_DB.Create(case_backup).Error
	return err
}

// DeleteCase_struct_backup 删除Case_struct_backup记录
// Author [piexlmax](https://github.com/piexlmax)
func (case_backupService *Case_struct_backupService)DeleteCase_struct_backup(case_backup case_library_backup.Case_struct_backup) (err error) {
	err = global.GVA_DB.Delete(&case_backup).Error
	return err
}

// DeleteCase_struct_backupByIds 批量删除Case_struct_backup记录
// Author [piexlmax](https://github.com/piexlmax)
func (case_backupService *Case_struct_backupService)DeleteCase_struct_backupByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]case_library_backup.Case_struct_backup{},"id in ?",ids.Ids).Error
	return err
}

// UpdateCase_struct_backup 更新Case_struct_backup记录
// Author [piexlmax](https://github.com/piexlmax)
func (case_backupService *Case_struct_backupService)UpdateCase_struct_backup(case_backup case_library_backup.Case_struct_backup) (err error) {
	err = global.GVA_DB.Save(&case_backup).Error
	return err
}

// GetCase_struct_backup 根据id获取Case_struct_backup记录
// Author [piexlmax](https://github.com/piexlmax)
func (case_backupService *Case_struct_backupService)GetCase_struct_backup(id uint) (case_backup case_library_backup.Case_struct_backup, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&case_backup).Error
	return
}

// GetCase_struct_backupInfoList 分页获取Case_struct_backup记录
// Author [piexlmax](https://github.com/piexlmax)
func (case_backupService *Case_struct_backupService)GetCase_struct_backupInfoList(info case_library_backupReq.Case_struct_backupSearch) (list []case_library_backup.Case_struct_backup, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&case_library_backup.Case_struct_backup{})
    var case_backups []case_library_backup.Case_struct_backup
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
    if info.Number != "" {
        db = db.Where("number LIKE ?","%"+ info.Number+"%")
    }
    if info.Manufacturing_serial_number != "" {
        db = db.Where("manufacturing_serial_number LIKE ?","%"+ info.Manufacturing_serial_number+"%")
    }
    if info.Troubleshooting_date != nil {
        db = db.Where("troubleshooting_date > ?",info.Troubleshooting_date)
    }
    if info.Process_design_document_number != "" {
        db = db.Where("process_design_document_number LIKE ?","%"+ info.Process_design_document_number+"%")
    }
    if info.Responsible_craftman != "" {
        db = db.Where("responsible_craftman = ?",info.Responsible_craftman)
    }
    if info.Fault_description != "" {
        db = db.Where("fault_description LIKE ?","%"+ info.Fault_description+"%")
    }
    if info.DISR_number != "" {
        db = db.Where("d_i_s_r_number LIKE ?","%"+ info.DISR_number+"%")
    }
    if info.Operarion_description1 != "" {
        db = db.Where("operation_description1 LIKE ?","%"+ info.Operarion_description1+"%")
    }
    if info.Operation_description2 != "" {
        db = db.Where("operation_description2 LIKE ?","%"+ info.Operation_description2+"%")
    }
    if info.Operarion_description3 != "" {
        db = db.Where("operation_description3 LIKE ?","%"+ info.Operarion_description3+"%")
    }
    if info.Operarion_description4 != "" {
        db = db.Where("operation_description4 LIKE ?","%"+ info.Operarion_description4+"%")
    }
    if info.Operation_description5 != "" {
        db = db.Where("operation_description5 LIKE ?","%"+ info.Operation_description5+"%")
    }
    if info.Ata_chapter_level1 != "" {
        db = db.Where("ata_chapter_level1 LIKE ?","%"+ info.Ata_chapter_level1+"%")
    }
    if info.Ata_chapter_level2 != "" {
        db = db.Where("ata_chapter_level2 LIKE ?","%"+ info.Ata_chapter_level2+"%")
    }
    if info.Conclusion != "" {
        db = db.Where("conclusion LIKE ?","%"+ info.Conclusion+"%")
    }
    if info.Complete_date != nil {
        db = db.Where("complete_date > ?",info.Complete_date)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	err = db.Limit(limit).Offset(offset).Find(&case_backups).Error
	return  case_backups, total, err
}
